﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MCSFramework.BLL.TEST;
using MCSFramework.Model.TEST;

public partial class TEST : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            //btnChg.Attributes["onClick"] = "return confirm('确定要更新该记录？', '确定', '取消')";

            GridView1.AllowPaging = true;
            GridView1.PageSize = 15;


            this.Bind();
        }
    }

    void Bind()
    {
        IList<TEST_Client> ClientList = TEST_ClientBLL.GetModelList("1=1");
        GridView1.DataSource = ClientList;
        GridView1.DataBind();
    }

    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "Choose")//点击了选择按钮
        {
            LinkButton linkBtn = e.CommandSource as LinkButton;
            GridViewRow row = linkBtn.Parent.Parent as GridViewRow;
            string id = row.Cells[1].Text;
            IList<TEST_Client> clientList = TEST_ClientBLL.GetModelList("id=" + id);

            TEST_Client client = clientList[0];
            ViewState["TEST_Client"] = client;

            txtName.Text = client.Name;
            txtTeleNum.Text = client.TeleNum;
            txtAddress.Text = client.Address;
            txtFax.Text = client.Fax;
            txtAddress.Text = client.Address;
            txtPostCode.Text = client.PostCode;
            txtProv.Text = client.Prov;
            txtCity.Text = client.City;
        }
    }
    protected void btnDelete_Click(object sender, EventArgs e)
    {
        TEST_ClientBLL clientBLL = new TEST_ClientBLL();
        for (int i = 0; i < GridView1.Rows.Count; i++)
        {
            CheckBox ck = (CheckBox)GridView1.Rows[i].FindControl("chkClient_ID");
            if (ck.Checked == true)
            {
                int a = int.Parse(GridView1.DataKeys[i].Value.ToString());
                int b = clientBLL.Delete(a);
            }
        }
        this.Bind();
    }
    protected void btnChg_Click(object sender, EventArgs e)
    {
        if (ViewState["TEST_Client"] != null)
        {           
            TEST_Client client = (TEST_Client)ViewState["TEST_Client"];
            client.Name = txtName.Text;
            client.Mobile = txtMobile.Text;
            client.TeleNum = txtTeleNum.Text;
            client.Address = txtAddress.Text;
            client.Fax = txtFax.Text;
            client.Address = txtAddress.Text;
            client.PostCode = txtPostCode.Text;
            client.Prov = txtProv.Text;
            client.City = txtCity.Text;

            TEST_ClientBLL clientBll = new TEST_ClientBLL();
            clientBll.Model = client;
            clientBll.Update();

            this.Bind();

        }
        
    }
    protected void btnAdd_Click(object sender, EventArgs e)
    {
        TEST_Client client = new TEST_Client();
        client.Name = txtName.Text;
        client.Mobile = txtMobile.Text;
        client.TeleNum = txtTeleNum.Text;
        client.Address = txtAddress.Text;
        client.Fax = txtFax.Text;
        client.Address = txtAddress.Text;
        client.PostCode = txtPostCode.Text;
        client.Prov = txtProv.Text;
        client.City = txtCity.Text;

        TEST_ClientBLL clientBll = new TEST_ClientBLL();
        clientBll.Model = client;
        clientBll.Add();

        this.Bind();
    }
}
